﻿using System.Collections.Generic;
using System.Xml;
using System.Text;
using System.Web;

namespace SkillBook.SearchBook
{
    public class GoogleSearchBook : ISearchBook
    {
        private const string googleURL = @"http://books.google.com/books/feeds/volumes?";
        private int start_index;
        private int max_results;

        public GoogleSearchBook(int index, int results)
        {
            start_index = index;
            max_results = results;
        }

        public IList<BookItem> SearchBook(string keyWord)
        {
            IList<BookItem> books = new List<BookItem>();

            //try
            //{
                //Build google search URL.
                StringBuilder sb = new StringBuilder();
                sb.Append(googleURL);
                sb.Append("q=");
                sb.Append(keyWord);
                sb.Append("&start-index=");
                sb.Append(start_index.ToString());
                sb.Append("&max-results=");
                sb.Append(max_results.ToString());

                XmlDocument returnXml = new XmlDocument();
                returnXml.Load(sb.ToString());

                XmlNamespaceManager mgr = new XmlNamespaceManager(returnXml.NameTable);
                mgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
                mgr.AddNamespace("dc", "http://purl.org/dc/terms");

                foreach (XmlNode item in returnXml.SelectNodes("//atom:entry", mgr))
                {
                    string ISBN = "";
                    string Title = "";
                    string Author = "";
                    string Link = "";
                    string CoverLink = "";
                    string Description = "";

                    #region ISBN List
                    StringBuilder sbISBN = new StringBuilder();
                    foreach (XmlNode nodeISBN in item.SelectNodes(@"dc:identifier", mgr))
                    {
                        sbISBN.Append(nodeISBN.InnerText);
                        sbISBN.Append(",");
                    }

                    if (sbISBN.Length > 0)
                    {
                        ISBN = sbISBN.ToString(0, sbISBN.Length - 1);
                    }
                    #endregion

                    #region Title
                    XmlNode nodeTitle = item.SelectSingleNode(@"atom:title[@type='text']", mgr);
                    if(nodeTitle != null)
                    {
                        Title = nodeTitle.InnerText;
                    }
                    #endregion

                    #region Author List
                    StringBuilder sbAuthor = new StringBuilder();
                    foreach (XmlNode nodeAuthor in item.SelectNodes(@"dc:creator", mgr))
                    {
                        sbAuthor.Append(nodeAuthor.InnerText);
                        sbAuthor.Append(",");
                    }

                    if (sbAuthor.Length > 0)
                    {
                        Author = sbAuthor.ToString(0, sbAuthor.Length - 1);
                    }
                    #endregion

                    #region Link
                    XmlNode nodeLink = item.SelectSingleNode(@"atom:link[@rel='http://schemas.google.com/books/2008/preview']/@href", mgr);
                    if (nodeLink != null)
                    {
                        Link = nodeLink.InnerText;
                    }
                    #endregion

                    #region Cover Link
                    XmlNode nodeCoverLink = item.SelectSingleNode(@"atom:link[@rel='http://schemas.google.com/books/2008/thumbnail']/@href", mgr);
                    if (nodeCoverLink != null)
                    {
                        CoverLink = nodeCoverLink.InnerText;
                    }
                    #endregion

                    #region Description List
                    StringBuilder sbDescription = new StringBuilder();
                    foreach (XmlNode nodeDescription in item.SelectNodes(@"dc:description", mgr))
                    {
                        sbDescription.Append(nodeDescription.InnerText);
                        sbDescription.Append(",");
                    }

                    if (sbDescription.Length > 0)
                    {
                        Description = sbDescription.ToString(0, sbDescription.Length - 1);
                    }
                    #endregion

                    books.Add(new BookItem(ISBN, Title, Author, Link, CoverLink, Description));
                }
            //}
            //catch
            //{
            //    books = null;
            //}

            return books;
        }
    }
}